# -*- coding: utf-8 -*-
import os

from loguru import logger

from utils.constant import LOG_PATH
from utils.timeoperator import timeoperator


class LogOperator:
    def __init__(self, name, path=LOG_PATH, level='DEBUG', isprint=True):
        self.name = name
        self.isprint = isprint
        self.log = logger
        self.log.remove()  # Remove default logger to customize
        self.log.add(lambda msg: print(msg, end="") if self.isprint else None, level=level)

        if path:
            if not os.path.isdir(path):
                os.mkdir(path)
            log_file = os.path.join(path, 'logging.log')
            self.log.add(log_file, rotation="1 day", retention="7 days", level=level,
                         format="{time} | {level} | {name} | {message}")

    def debug(self, message):
        self.log.debug(f"[{timeoperator.now1}] | <{self.name}> | {message}")

    def info(self, message):
        self.log.info(f"[{timeoperator.now1}] | <{self.name}> | {message}")

    def warning(self, message):
        self.log.warning(f"[{timeoperator.now1}] | <{self.name}> | {message}")

    def error(self, message):
        self.log.error(f"[{timeoperator.now1}] | <{self.name}> | {message}")

    def exception(self, message):
        self.log.exception(f"[{timeoperator.now1}] | <{self.name}> | {message}")

    def critical(self, message):
        self.log.critical(f"[{timeoperator.now1}] | <{self.name}> | {message}")


if __name__ == '__main__':
    logger = LogOperator("MyLogger")
    logger.debug('debug')
    logger.info('info')
    logger.warning('warning')
    logger.error('error')
    logger.critical('CRITICAL')
